﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Areas.Pedagogie.Models;

namespace SchoolProject2012.Areas.Pedagogie.Controllers
{
    [Authorize(Roles = "Admin, Pedagogie")]
    public class CourController : Controller
    {
        private SchoolProjectBddEntities1 db = new SchoolProjectBddEntities1();

        //
        // GET: /Pedagogie/Cour/

        public ActionResult Index()
        {
            var cours = db.COURS.Include("PROFESSEUR").Include("SALLE");
            return View(cours.ToList());
        }

        //
        // GET: /Pedagogie/Cour/Details/5

        public ActionResult Details(int id = 0)
        {
            COURS cours = db.COURS.Single(c => c.ID_COURS == id);
            if (cours == null)
            {
                return HttpNotFound();
            }
            return View(cours);
        }

        //
        // GET: /Pedagogie/Cour/Create

        public ActionResult Create()
        {
            ViewBag.LOGINPERS = new SelectList(db.PROFESSEUR, "LOGINPERS", "COMPETENCES_PROF");
            ViewBag.IDSALLE = new SelectList(db.SALLE, "IDSALLE", "LIBSALLE");
            return View();
        }

        //
        // POST: /Pedagogie/Cour/Create

        [HttpPost]
        public ActionResult Create(COURS cours)
        {
            if (ModelState.IsValid)
            {
                db.COURS.AddObject(cours);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.LOGINPERS = new SelectList(db.PROFESSEUR, "LOGINPERS", "COMPETENCES_PROF", cours.LOGINPERS);
            ViewBag.IDSALLE = new SelectList(db.SALLE, "IDSALLE", "LIBSALLE", cours.IDSALLE);
            return View(cours);
        }

        //
        // GET: /Pedagogie/Cour/Edit/5

        public ActionResult Edit(int id = 0)
        {
            COURS cours = db.COURS.Single(c => c.ID_COURS == id);
            if (cours == null)
            {
                return HttpNotFound();
            }
            ViewBag.LOGINPERS = new SelectList(db.PROFESSEUR, "LOGINPERS", "COMPETENCES_PROF", cours.LOGINPERS);
            ViewBag.IDSALLE = new SelectList(db.SALLE, "IDSALLE", "LIBSALLE", cours.IDSALLE);
            return View(cours);
        }

        //
        // POST: /Pedagogie/Cour/Edit/5

        [HttpPost]
        public ActionResult Edit(COURS cours)
        {
            if (ModelState.IsValid)
            {
                db.COURS.Attach(cours);
                db.ObjectStateManager.ChangeObjectState(cours, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.LOGINPERS = new SelectList(db.PROFESSEUR, "LOGINPERS", "COMPETENCES_PROF", cours.LOGINPERS);
            ViewBag.IDSALLE = new SelectList(db.SALLE, "IDSALLE", "LIBSALLE", cours.IDSALLE);
            return View(cours);
        }

        //
        // GET: /Pedagogie/Cour/Delete/5

        public ActionResult Delete(int id = 0)
        {
            COURS cours = db.COURS.Single(c => c.ID_COURS == id);
            if (cours == null)
            {
                return HttpNotFound();
            }
            return View(cours);
        }

        //
        // POST: /Pedagogie/Cour/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            COURS cours = db.COURS.Single(c => c.ID_COURS == id);
            db.COURS.DeleteObject(cours);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Tuiles()
        {
            return View();
        }
    }
}